#include <stdbool.h>
#include <assert.h>
#include <unistd.h>

typedef int mutex_t;

class MutexLocker {
	MutexLocker(mutex_t *pmut):
		pmut_ (pmut) {
		assert(pmut);
		while (!__sync_bool_compare_and_swap(pmut_, 0, 1))
			sleep(0);
	}

	~MutexLocker() {
		*pmut_ = 0;
	}
private:
	mutex_t * pmut_;
};
